﻿//---------- 获取第一地址信息--------------
function SelCity(obj,e) {
    var ths = obj;
    var dal = '<div class="_citys" style="margin-top:1px;"><span title="关闭" id="cColse" >×</span><ul id="_citysheng" class="_citys0"><li class="citySel">省份</li> '+
                   '</ul>'+'<div style="margin-top:1px;"> <input type="text" oninput="chooseShengFen(this.value)" class="input-text radius" style="width:130px;" id="provInput" />&nbsp;<a width="24px" href="javascript:void(0);" height="24px" onClick="addProv()">添加新省份</a></div> '+
                   '<div id="_citys0" class="_citys1"></div><div style="display:none" id="_citys1" class="_citys1"></div><div style="display:none" id="_citys2" class="_citys1"></div></div>';//<li>城市</li><li>县区</li>
    Iput.show({ id: ths, event: e, content: dal,width:"470"});
    $("#cColse").click(function () {
        Iput.colse();
    });
    var tb_province = [];
    var b = province;
    if(b && b.length) {
		for (var i = 0, len = b.length; i < len; i++) {
		    tb_province.push('<a data-level="0" data-id="' + b[i]['provId'] + '" data-name="' + b[i]['provName'] + '">' + b[i]['provName'] + '</a>');
		}
    }
    $("#_citys0").append(tb_province.join(""));
 
   //------------------------
    provChooseClick();
   
}
 
//---------- 获取第二地址信息--------------
function SelCity2(obj,e) {
    var ths = obj;
    var dal = '<div class="_citys" style="margin-top:1px;"><span title="关闭" id="cColse" >×</span><ul id="_citysheng" class="_citys0"><li class="citySel">市区</li> '+
       '</ul>'+'<div style="margin-top:1px;"> <input type="text" oninput="chooseCity(this.value )" class="input-text radius" style="width:130px;" id="cityInput" />&nbsp;<a width="24px" href="javascript:void(0);" height="24px" onClick="addCity()">添加新市区</a></div> '+
       '<div id="_citys0" class="_citys1"></div><div style="display:none" id="_citys1" class="_citys1"></div><div style="display:none" id="_citys2" class="_citys1"></div></div>';//<li>城市</li><li>县区</li>   
 
    Iput.show({ id: ths, event: e, content: dal,width:"470"});
    $("#cColse").click(function () {
        Iput.colse();
    });
    var tb_province = [];
    var b = city;
    if(b && b.length) {
    	for (var i = 0, len = b.length; i < len; i++) {
            if(b[i]['pid'] ==choose_provid){  }
             tb_province.push('<a data-level="0" data-id="' + b[i]['id'] + '" data-name="' + b[i]['cityName'] + '">' + b[i]['cityName'] + '</a>');
        }
    }
    $("#_citys0").append(tb_province.join(""));
    cityChooseClick();
}
 
//---------- 获取第三地址信息--------------
function SelCity3(obj,e){
    var ths = obj;
    var dal = '<div class="_citys" style="margin-top:1px;"><span title="关闭" id="cColse" >×</span><ul id="_citysheng" class="_citys0"><li class="citySel">县区</li> '+
       '</ul>'+'<div style="margin-top:1px;"> <input type="text" oninput="chooseXianji(this.value)" class="input-text radius" style="width:130px;" id="areaInput" />&nbsp;<a width="24px" href="javascript:void(0);" height="24px" onClick="addArea()">添加新县区</a></div> '+
       '<div id="_citys0" class="_citys1"></div><div style="display:none" id="_citys1" class="_citys1"></div><div style="display:none" id="_citys2" class="_citys1"></div></div>';//<li>城市</li><li>县区</li>
    Iput.show({ id: ths, event: e, content: dal,width:"470"});
    $("#cColse").click(function () {
        Iput.colse();
    });
    var tb_province = [];
    var b = area;
    if(b && b.length) {
    	for (var i = 0, len = b.length; i < len; i++) {
            tb_province.push('<a data-level="0" data-id="' + b[i]['areaId'] + '" data-name="' + b[i]['areaName'] + '">' + b[i]['areaName'] + '</a>');
        }
    }
    $("#_citys0").append(tb_province.join(""));
    areaChooseClick();
}
 
function getCity(obj) {
    var c = obj.data('id');
    var e = province;
    var f;
    var g = '';
    for (var i = 0, plen = e.length; i < plen; i++) {
        if (e[i]['id'] == parseInt(c)) {
            f = e[i]['city'];
            break
        }
    }
   
    $("#_citysheng li").removeClass("citySel");
    $("#_citysheng li:eq(1)").addClass("citySel");
    return g;
}
function getArea(obj) {
    var c = obj.data('id');
    var e = area;
    var f = [];
    var g = '';
    for (var i = 0, plen = e.length; i < plen; i++) {
        if (e[i]['pid'] == parseInt(c)) {
            f.push(e[i]);
        }
    }
 
    $("#_citysheng li").removeClass("citySel");
    $("#_citysheng li:eq(2)").addClass("citySel");
    return g;
}
 
 
//异步请求区域地址
function getAjaxRaea(){
 
    var url =path+"/address/address_area_list";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : { cityID: choose_cityid    },             
              success : function(data){
                area = data;
             
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    }
 
//异步请求区域地址
function getAjaxCity(){
 
    var url =path+"/address/address_city_list";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : { provID: choose_provid },             
              success : function(data){
                city = data;
               area=[];
               //var data = eval("("+data+")");
           
             //var tablevaljson = eval(  data );
            
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    }
if(province == null){
    
    getAjaxProv();
}
//异步请求区域地址
function getAjaxProv(){
    var url = path+"/address/address_prov_list";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : {   },     // cityID: choose_cityid         
              success : function(data){
               province = data;
            
               //var data = eval("("+data+")");
                
             //var tablevaljson = eval(  data );
            
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    }
 
//省区点击事件
function provChooseClick(){
$("#_citys0 a").click(function () {
   
    var lev = $(this).data("name");
    $("#city").val( $(this).data("name"));
    
    choose_provid =  $(this).data("id");
    choose_provname= $(this).data("name");
    
    choose_cityid = 0;
    choose_cityname= "";
    choose_areaid =0;
    choose_areaname ="";
    
    $("#city2").val("");
    $("#city3").val("");
    $("#cColse").click();
 //---------
    getAjaxCity();//查询城市地址
});
 
}
 
//省区点击事件
function cityChooseClick(){
    
$("#_citys0 a").click(function () {
   
    var lev = $(this).data("name");
    $("#city2").val( $(this).data("name"));
    
    choose_cityid =  $(this).data("id");
    choose_cityname= $(this).data("name");
    choose_areaid =0;
    choose_areaname ="";
    
    $("#city3").val("");
  $("#cColse").click();
 //---------
  getAjaxRaea();//查询三级地址
});
 
 
}
//省区点击事件
function areaChooseClick(){
$("#_citys0 a").click(function () {
   
    var lev = $(this).data("name");
    $("#city3").val( $(this).data("name"));
    
    choose_areaid =  $(this).data("id");
    choose_areaname= $(this).data("name");
   
  $("#cColse").click();
 //---------
  //getAjaxCity();//查询城市地址
});
 
}
 
// z实时获取value 文本
function chooseShengFen(value) {
    if(value.length==0){
             $("#_citys0").remove();
            var tb_province = [];
            var b= province;   
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                tb_province.push('<a data-level="0" data-id="' + b[i]['provId'] + '" data-name="' + b[i]['provName'] + '">' + b[i]['provName'] + '</a>');
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            provChooseClick(); 
    }else{
         $("#_citys0").remove();
         var tb_province = [];
            var b = province;
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                 var pinying =makePy(b[i]['provName'])+"";
                // layer.alert(pinying+" - "+ pinying.indexOf(value.toUpperCase()) +"  to"+value );
                 if(b[i]['provName'].indexOf(value)>=0 ||( pinying.indexOf(value.toUpperCase()))>=0){
                     tb_province.push('<a data-level="0" data-id="' + b[i]['provId'] + '" data-name="' + b[i]['provName'] + '">' + b[i]['provName'] + '</a>');
                 }
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            
            provChooseClick();
        
    }
     /*  for (var i = 0, len = b.length; i < len; i++) {
            if(value.indexOf(b[i]['areaName'])!=-1 ){
                tb_province.push('<a data-level="0" data-id="' + b[i]['areaId'] + '" data-name="' + b[i]['areaName'] + '">' + b[i]['areaName'] + '</a>');
            }
        }*/
      
       // $("#_citys0").append(tb_province.join("")); 
}
 
 
//z实时获取value 文本
function chooseCity(value) {
    if(value.length==0){
             $("#_citys0").remove();
            var tb_province = [];
            var b= city;   
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                 if(b[i]['provId'] ==choose_provid){
                     
                     tb_province.push('<a data-level="0" data-id="' + b[i]['id'] + '" data-name="' + b[i]['cityName'] + '">' + b[i]['cityName'] + '</a>');
                    
                    }
                //tb_province.push('<a data-level="0" data-id="' + b[i]['id'] + '" data-name="' + b[i]['name'] + '">' + b[i]['name'] + '</a>');
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            
            cityChooseClick(); 
            
           
    }else{
         $("#_citys0").remove();
         var tb_province = [];
            var b = city;
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                 var pinying =makePy(b[i]['cityName'])+"";
                // layer.alert(pinying+" - "+ pinying.indexOf(value.toUpperCase()) +"  to"+value );
                 if(b[i]['provId'] !=choose_provid){ continue;}
                 if(b[i]['cityName'].indexOf(value)>=0 ||( pinying.indexOf(value.toUpperCase()))>=0){
                     tb_province.push('<a data-level="0" data-id="' + b[i]['id'] + '" data-name="' + b[i]['cityName'] + '">' + b[i]['cityName'] + '</a>');
                 }
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            
            cityChooseClick();
        
    }
     
}
     
 
//z实时获取value 文本
function chooseXianji(value) {
 
    if(value.length==0){
             $("#_citys0").remove();
            var tb_province = [];
            var b= area;   
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                tb_province.push('<a data-level="0" data-id="' + b[i]['areaId'] + '" data-name="' + b[i]['areaName'] + '">' + b[i]['areaName'] + '</a>');
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            areaChooseClick(); 
    }else{
         
         $("#_citys0").remove();
         var tb_province = [];
            var b = area;
            var div_s ='<div id="_citys0" class="_citys1">';
            tb_province.push(div_s);
            for (var i = 0, len = b.length; i < len; i++) {
                 var pinying =makePy(b[i]['areaName'])+"";
           
                // layer.alert(pinying+" - "+ pinying.indexOf(value.toUpperCase()) +"  to"+value );
                 if(b[i]['areaName'].indexOf(value)>=0 ||( pinying.indexOf(value.toUpperCase()))>=0){
                     tb_province.push('<a data-level="0" data-id="' + b[i]['areaId'] + '" data-name="' + b[i]['areaName'] + '">' + b[i]['areaName'] + '</a>');
                 }
            }
            var div_e ='</div>';
            tb_province.push(div_e);
            $("._citys").append(tb_province.join(""));
            
            areaChooseClick();
        
    }
     
}
 
//添加 省区 地址
function addProv(){
    
    var provValue = $("#provInput").val();
    if(provValue.trim()=="" ){
         layer.alert("地址不能为空！");
         return;
    }
    var b= province;
    for (var i = 0; i < province.length; i++) {
        var pinying =makePy(b[i]['provName'])+"";
          
         if(b[i]['provName'].indexOf(provValue)>=0 ||( pinying.indexOf(provValue.toUpperCase()))>=0){
             
             layer.alert("地址已存在，添加失败！");
             return;
         }
        
    } 
    //-------------------  add prov ---------------------
    //
    layer.confirm("是否确定添加此新省份？", function() {
    	addProvAjax(provValue);
    });
}
 
 
//添加 市区 地址
function addCity(){
    
    var cityValue = $("#cityInput").val();
    if(cityValue.trim()=="" ){
         layer.alert("地址不能为空！");
         return;
    }
    var b= city;
    for (var i = 0; i < city.length; i++) {
        var pinying =makePy(b[i]['cityName'])+"";
       
       if(b[i]['cityName'].indexOf(cityValue)>=0 ||( pinying.indexOf(cityValue.toUpperCase()))>=0){
           layer.alert("地址已存在，添加失败！");
           return;
       }
        
    } 
    //-------------------  add prov ---------------------
    //
    layer.confirm("是否确定添加此新市区？", function() {
    	addCityAjax(cityValue);
    });
}
 
 
//添加 县级 地址
function addArea(){
    
    var cityValue = $("#areaInput").val();
    if(cityValue.trim()=="" ){
         layer.alert("地址不能为空！");
         return;
    }
    var b= area;
    for (var i = 0; i < area.length; i++) {
        var pinying =makePy(b[i]['areaName'])+"";
     
        //b[i]['provName']+" - "+b[i]['provId']
     if(b[i]['areaName'].indexOf(cityValue)>=0 ||( pinying.indexOf(cityValue.toUpperCase()))>=0){
         
         layer.alert("地址已存在，添加失败！");
         return;
     }
        
    } 
    //-------------------  add prov ---------------------
    layer.confirm("是否确定添加此新县区？", function() {
    	addAreaAjax(cityValue);
    });
}
function addProvAjax(provValue){
    
    var url =path+"/address/address_prov_add";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : { provName:provValue },     // cityID: choose_cityid         
              success : function(data){           
                   layer.alert("添加成功！");  
               //var data = eval("("+data+")");
             //var tablevaljson = eval(  data );
               window.location.reload();
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    
    
}
 
function addCityAjax(cityValue){
    if(choose_provid==0){
        layer.alert("请选择省份！");
        return;
    }
    var url =path+"/address/address_city_add";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : { cityName:cityValue, provID:choose_provid},     // cityID: choose_cityid         
              success : function(data){               
               layer.alert("添加成功！");  
               //var data = eval("("+data+")");
             //var tablevaljson = eval(  data );
               window.location.reload();
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    
}
function addAreaAjax(cityValue){
    if(choose_cityid==0){
        layer.alert("请选择市区！");
        return;
    }
    var url =path +"/address/address_area_add";
    $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : { areaName:cityValue, cityID:choose_cityid},     // cityID: choose_cityid         
              success : function(data){               
               //var str = JSON.stringify(data);             
               layer.alert("添加成功！");  
               window.location.reload();
               //var data = eval("("+data+")");
             //var tablevaljson = eval(  data );
            
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    
}
 
 
//------ 删除地址 -------
function delProv(){
    if(choose_provid==0||!$("#city").val()){
        layer.alert("请选择省份！");
        return;
    }
    layer.confirm("是否确定删除此省份？", function() {
    	var url =path+"/address/address_prov_del";
        $.ajax({
               url : url,
                  type : "post",
                  dataType : "json",    
                  data : {  provID:choose_provid},     // cityID: choose_cityid         
                  success : function(data){                
                   layer.alert("删除成功！");  
                   //var data = eval("("+data+")");
                 //var tablevaljson = eval(  data );
                   window.location.reload();
                 },
              error : function(){
                  layer.alert("操作失败！");
              }
           });
    });
}
//------ 删除地址 -------
function delCity(){
    
    if(choose_cityid==0||!$("#city2").val()){
        layer.alert("请选择市区！");
        return;
    }
    layer.confirm("是否确定删除此市区？", function() {
    	var url =path +"/address/address_city_del";
        $.ajax({
           url : url,
              type : "post",
              dataType : "json",    
              data : {cityID:choose_cityid , provID:choose_provid},     // cityID: choose_cityid         
              success : function(data){           
                   layer.alert("删除成功！");  
               //var data = eval("("+data+")");
             //var tablevaljson = eval(  data );
               window.location.reload();
             },
          error : function(){
              layer.alert("操作失败！");
          }
       });
    });
}
// ------ 删除地址 -------
function delArea(){
    if(choose_areaid==0||!$("#city3").val()){
        layer.alert("请选择县区！");
        return;
    }
    layer.confirm("是否确定删除此县区？", function() {
    	 var url =path+"/address/address_area_del";
    	    $.ajax({
	           url : url,
	              type : "post",
	              dataType : "json",    
	              data : {cityID:choose_cityid , areaID:choose_areaid},     // cityID: choose_cityid         
	              success : function(data){           
	                   layer.alert("删除成功！");  
	               //var data = eval("("+data+")");
	             //var tablevaljson = eval(  data );
	               window.location.reload();
	             },
	          error : function(){
	              layer.alert("操作失败！");
	          }
	       });
    });
}
 
function makePy(str)  
{    
        if(typeof(str) != "string")    
        throw new Error(-1,"函数makePy需要字符串类型参数！");    
        var arrResult = new Array();   
        //将字符串转码后转为数组  
        for(var i=0,len=str.length;i<len;i++)  
        {    
            var ch = str.charAt(i);    
            arrResult.push(checkCh(ch));    
        }    
        return mkRslt(arrResult);    
}    
//测试程序  
function getIndexChanaChar()  
{  
    var str = document.getElementById("text").value;  
    layer.alert(makePy(str));  
}  
  
  
function checkCh(ch)  
{    
    var uni = ch.charCodeAt(0);    
    //如果不在汉字处理范围之内,返回原字符,也可以调用自己的处理函数    
    if(uni > 40869 || uni < 19968)    
    return ch; //dealWithOthers(ch);    
    //检查是否是多音字,是按多音字处理,不是就直接在strChineseFirstPY字符串中找对应的首字母    
    return (oMultiDiff[uni]?oMultiDiff[uni]:(strChineseFirstPY.charAt(uni-19968)));    
}    
function mkRslt(arr)  
{    
    var arrRslt = [""];    
    for(var i=0,len=arr.length;i<len;i++)  
    {    
        var str = arr[i];    
        var strlen = str.length;    
        if(strlen == 1)  
        {    
            for(var k=0;k<arrRslt.length;k++)  
            {    
                arrRslt[k] += str;    
            }    
        }  
        else  
        {    
            var tmpArr = arrRslt.slice(0);    
            arrRslt = [];    
            for(k=0;k<strlen;k++)  
            {    
                //复制一个相同的arrRslt    
                var tmp = tmpArr.slice(0);    
                //把当前字符str[k]添加到每个元素末尾    
                for(var j=0;j<tmp.length;j++)  
                {    
                    tmp[j] += str.charAt(k);    
                }    
                //把复制并修改后的数组连接到arrRslt上    
                arrRslt = arrRslt.concat(tmp);    
            }    
        }    
    }    
    return arrRslt;    
}   
 